package com.shopcz.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.shopcz.dbcon.DBConnection;
import com.shopcz.pojo.OrderDetails;
import com.shopcz.pojo.User;
import com.shopcz.view.OrderDetailsView;

/**
 * 
 * @author mx
 *
 */
public class OrderDetailDao {
	 public List<OrderDetailsView> queryCarByUserId(int userId){
		List<OrderDetailsView> carList = new ArrayList<OrderDetailsView>();
		String sql="select DISTINCT gds.goodsImg,gds.goodsName," +
				"gds.goodsPrice,car.SHOPNUM,us.UserName,us.USEREMAIL,gds.Goodsid " +
				"from userer us,GOODS gds,SHOPCAR car " +
				"where car.GOODSID=gds.Goodsid and car.SHOPSTATUS=3 " +
				"and us.USERID=gds.UserId and car.Useridbuy=?";
		Connection con = DBConnection.getDBConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setInt(1,userId);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
			  OrderDetailsView carinfo = new OrderDetailsView();
			  carinfo.setGoodsImg(rs.getString(1));
			  carinfo.setConName(rs.getString(2));
			  carinfo.setGoodsPrice(rs.getDouble(3));
			  carinfo.setShopNum(rs.getInt(4));
			  carinfo.setUserName(rs.getString(5));
			  carinfo.setUseremail(rs.getString(6));
			  carinfo.setGoodsId(rs.getInt(7));
			  carList.add(carinfo);
			}
			pst.close();
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DBConnection.closeDBConnection(con);
		return carList;
	}
	 
	 public User queryByUserId(int userid){
		 User user=null;
		 String sql="select DISTINCT us.UserName,us.useremail from ORDERS ord ," +
		 		"userer us where us.USERID=(select USERSELLNO from " +
		 		"ORDERS where USERBUYNO=?)";
		 Connection con = DBConnection.getDBConnection();
		 PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql);
			pst.setInt(1,userid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				user = new User();
				user.setUserName(rs.getString(1));
				user.setUseremail(rs.getString(2));	
			}
			pst.close();
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		DBConnection.closeDBConnection(con);
		return user;
	 }
	
	 public boolean save(OrderDetails ord){
		 boolean flag=false;
		 String sql="insert into orderdetails values(seq_orderdetail.nextVal,?,?,?,?,?)";
		 Connection con =DBConnection.getDBConnection();
		try {
			 PreparedStatement pst = con.prepareStatement(sql);
			 pst.setInt(1, ord.getOrderNo());
			 pst.setInt(2, ord.getComNum());
			 pst.setDouble(3, ord.getUnitPrice());
			 pst.setInt(4, ord.getQuantity());
			 pst.setString(5, ord.getComName());
			 if(pst.executeUpdate()>0){
					flag=true;
			 }
			pst.close();
				
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		DBConnection.closeDBConnection(con);
		return flag;
 
	 }

}
